package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0
 * @package com.itheima.service
 * @description:
 * @author: Ewaits
 * @date: 2019/7/5 16:38
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        setRelationMenuAndRole(role.getId(), menuIds);
        setRelationPermissionAndRole(role.getId(), permissionIds);
    }

    private void setRelationPermissionAndRole(Integer id, Integer[] permissionIds) {
        if (permissionIds != null && permissionIds.length > 0) {
            List<Map<String, Integer>> list = new ArrayList<>();
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("permissionId", permissionId);
                map.put("roleId", id);
                list.add(map);
            }
            roleDao.setRelationPermissionAndRole(list);
        }
    }

    private void setRelationMenuAndRole(Integer id, Integer[] menuIds) {
        if (menuIds != null && menuIds.length > 0) {
            List<Map<String, Integer>> list = new ArrayList<>();

            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("menuId", menuId);
                map.put("roleId", id);
                list.add(map);
            }
            roleDao.setRelationMenuAndRole(list);
        }
    }

    @Override
    public Role findRoleById(Integer id) {
        return roleDao.findRoleById(id);
    }

    @Override
    public Integer[] findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    @Override
    public Integer[] findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }

    @Override
    public void deleteRoleById(Integer id) {
        Long count = roleDao.findCountByRoleId(id);
        if (count > 0) {
            throw new RuntimeException("角色被引用");
        }
        roleDao.deleteRelationRoleAndPermission(id);
        roleDao.deleteRelationRoleAndMenu(id);
        roleDao.deleteRoleById(id);
    }

    @Override
    public void edit(Role role, Integer[] menuIds, Integer[] permissionIds) {
        roleDao.update(role);
        roleDao.deleteRelationRoleAndMenu(role.getId());
        roleDao.deleteRelationRoleAndPermission(role.getId());
        setRelationMenuAndRole(role.getId(), menuIds);
        setRelationPermissionAndRole(role.getId(),permissionIds);
    }
}
